package geektime.spring.springbucks;

import com.github.pagehelper.PageInfo;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.redis.JedisServiceImpl;
import geektime.spring.springbucks.redis.JedisUtils;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
public class SpringBucksApplication implements ApplicationRunner {
	@Autowired
	private CoffeeService coffeeService;

	@Autowired
	JedisServiceImpl jedisService;

	private static String REDIS_KEY="USER_ID";



	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {

		Coffee condition = new Coffee();
		//分页查询
		coffeeService.findAllWithParam(1, 3,condition)
				.forEach(c -> log.info("Page(1) Coffee {}", c));
		List<Coffee> list = coffeeService.findAllWithParam(2, 3,condition);
		PageInfo page = new PageInfo(list);
		log.info("PageInfo: {}", page);

		log.info("===================");
		//新增
		Coffee coffee = Coffee.builder().name("美式咖啡").price(new BigDecimal(20)).build();
		coffeeService.addCoffee(coffee);
		list = coffeeService.findAllWithParam(2, 3,condition);
		  page = new PageInfo(list);
		log.info("PageInfoAdd: {}", page);

		log.info("===================");

		//更新
//		Coffee updateCoffee = Coffee.builder().name("美式冷咖啡").price(Money.of(CurrencyUnit.of("CNY"), 20)).build();
		Coffee updateCoffee = Coffee.builder().name("美式冷咖啡").price(new BigDecimal(20)).build();
		updateCoffee.setId(4l);
		coffeeService.update(updateCoffee);
		list = coffeeService.findAllWithParam(2, 3,condition);
		  page = new PageInfo(list);
		log.info("PageInfoUpdate: {}", page);

		log.info("===================");

		//删除
		coffeeService.delById(4l);
		list = coffeeService.findAllWithParam(2, 3,condition);
		page = new PageInfo(list);
		log.info("PageInfoDelete: {}", page);

		log.info("===================");

		//根据主键查询
		coffee = coffeeService.getById(1l);
		log.info("CoffeeInfoById: {}", coffee);
		coffee = coffeeService.getById(1l);
		log.info("CoffeeInfoById2: {}", coffee);

		//根据主键批量查询查询
		List ids = new ArrayList();
		ids.add(1l) ;
		ids.add(2l) ;
		condition.setIds(ids);
		list = coffeeService.findAllWithParam(1, 3,condition);
		page = new PageInfo(list);
		log.info("CoffeeInfoByIds: {}", page);


		//生成全局唯一ID
		long jedisId= jedisService.nextId();
		log.info("Redis 全局唯一ID",jedisId);

		//ID去重
		if(jedisService.getBit(REDIS_KEY,jedisId)){
			log.info(jedisId+"存在");
		}else{
			log.info(jedisId+"不存在");
			jedisService.setBit(REDIS_KEY,jedisId,true);
		}
	}

}

